//
//  ZXFHomeController.m
//  163News
//
//  Created by Eden on 25/2/16.
//  Copyright © 2016年 Eden. All rights reserved.
//
#define RandomColor  [UIColor colorWithRed:arc4random() %256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1]

#import "ZXFHomeController.h"
#import "ZXFSocietyController.h"
#import "ZXFTitleLable.h"
@interface ZXFHomeController ()  <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *titleScrollView;
@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;
@end

@implementation ZXFHomeController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    [self setupConfig];
    
    [self setupChildVc];
    
    [self setupTitle];
    
    // 默认显示第一个控制器
    [self scrollViewDidEndScrollingAnimation:self.contentScrollView];
}
- (void)setupChildVc
{
    ZXFSocietyController *vc0 = [[ZXFSocietyController alloc] init];
    vc0.title = @"头条";
    [self addChildViewController:vc0];
    
    ZXFSocietyController *vc1 = [[ZXFSocietyController alloc] init];
    vc1.title = @"娱乐";
    [self addChildViewController:vc1];
    
    ZXFSocietyController *vc2 = [[ZXFSocietyController alloc] init];
    vc2.title = @"热门";
    [self addChildViewController:vc2];
    
    ZXFSocietyController *vc3 = [[ZXFSocietyController alloc] init];
    vc3.title = @"图片";
    [self addChildViewController:vc3];
    
    ZXFSocietyController *vc4 = [[ZXFSocietyController alloc] init];
    vc4.title = @"NBA";
    [self addChildViewController:vc4];
    
    ZXFSocietyController *vc5 = [[ZXFSocietyController alloc] init];
    vc5.title = @"体育";
    [self addChildViewController:vc5];
    
    ZXFSocietyController *vc6 = [[ZXFSocietyController alloc] init];
    vc6.title = @"社会";
    [self addChildViewController:vc6];
    
}
- (void)setupTitle
{
    CGFloat navH = self.navigationController.navigationBar.frame.size.height;

    CGFloat labelY = navH + 20;
    CGFloat labelH = self.titleScrollView.frame.size.height - 64;
    CGFloat labelW = 100;
    
    NSInteger childVCCOunt = self.childViewControllers.count;
    
    for (NSInteger i  = 0; i < childVCCOunt; i ++) {
        ZXFTitleLable *title_label = [[ZXFTitleLable alloc] init];
        [self.titleScrollView addSubview:title_label];
        title_label.tag = i;
        title_label.frame = CGRectMake(i * labelW, labelY, labelW, labelH);
        title_label.text = [self.childViewControllers[i] title];
        [title_label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleLabelTapClick:)]];
        
        if (i == 0 ) {
            title_label.scale = 1.0;
        }
    }
    
}

- (void)titleLabelTapClick:(UITapGestureRecognizer *)tap
{
    
    NSInteger labelIndex = tap.view.tag;
    
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = labelIndex * self.contentScrollView.frame.size.width;
    
    [self.contentScrollView setContentOffset:offset animated:YES];

}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    CGFloat  scrollW = scrollView.frame.size.width;
    CGFloat  scrollH = scrollView.frame.size.height;
    CGFloat  scrollOffsetX = scrollView.contentOffset.x;
    NSInteger index =  scrollOffsetX / scrollW;  //所点label的索引
    
    //1.找出所点的label
    ZXFTitleLable *label = self.titleScrollView.subviews[index];
    CGPoint titleOffset = self.titleScrollView.contentOffset;
    titleOffset.x = label.center.x - scrollW * 0.5;
    
    //处理"左边“超出offset
    if (titleOffset.x < 0) titleOffset.x = 0;
    
    //处理"右边“超出offset
    CGFloat rightMaxOffset =  self.titleScrollView.contentSize.width - scrollW;
    if (titleOffset.x > rightMaxOffset) titleOffset.x = rightMaxOffset;
    
    [self.titleScrollView setContentOffset:titleOffset animated:YES];
    
    
    
    // 1.bug 最后的插曲,让非选中的label的scanle值为0
    for (ZXFTitleLable *otherLabel  in self.titleScrollView.subviews) {
        if (otherLabel != label)
        otherLabel.scale = 0.0;
    }
    
    
    UIViewController *willVc = self.childViewControllers[index];
    
    if ([willVc isViewLoaded]) return;  // 如果已经加载view就不需要再设置view的frame。
    
    willVc.view.frame = CGRectMake(scrollOffsetX, 0, scrollW, scrollH);
    [scrollView addSubview:willVc.view];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scale = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 2.bug
    if (scale < 0 || scale > self.titleScrollView.subviews.count - 1) return;
    
    NSInteger leftIndex = scale;
    NSInteger rightIndex = leftIndex + 1;
    
    CGFloat rightScale = scale - leftIndex;
    CGFloat leftScale = 1 - rightScale;
    
    
   ZXFTitleLable *left_Lable =  self.titleScrollView.subviews[leftIndex];
    
    ZXFTitleLable *right_Lable = (rightIndex == self.titleScrollView.subviews.count) ? nil: self.titleScrollView.subviews[rightIndex];
    
    
    left_Lable.scale = leftScale;
    right_Lable.scale = rightScale;
    
}
- (void)setupConfig
{
    self.title = @"163.com新闻";
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    NSInteger childVCCOunt = self.childViewControllers.count;
    CGFloat labelW = [self.titleScrollView.subviews objectAtIndex:1].frame.size.width;
    self.titleScrollView.contentSize = CGSizeMake(childVCCOunt * labelW, 0);
    self.contentScrollView.contentSize = CGSizeMake(childVCCOunt * self.contentScrollView.frame.size.width, 0);
}

@end
